package com.itlong.cloud.controller.property;


import com.itlong.cloud.POJO.DTO.operate.OperatePushInfoPageDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateSavePushInfoDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperatePushInfoOneVO;
import com.itlong.cloud.POJO.VO.operate.OperatePushInfoVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.annotation.UnAuthAccessRequired;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.PushInfoTypeEnum;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.project.operate.IOperatePushInfoServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      物业—物业管理模块—物业通知。
 * <desc/>
 *
 * @createDate 2017-10-30
 */
@RestController
@RequestMapping("project/pushInfo")
public class PropertyPushInfoController extends BaseController {

    private static final Logger LOG = LoggerFactory.getLogger(PropertyPushInfoController.class);

    //消息推送
    @Autowired
    IOperatePushInfoServiceClient iOperatePushInfoServiceClient;

    /**
     * <desc>
     *      保存消息推送内容。
     * <desc/>
     *
     * @param  pushInfoDTO  添加消息对象
     * @return
     * @author ZiXuan
     * @createDate 2017-10-30
     */
    @LoggerInfo(operateDesc = LogDescConstant.SAVE_PUSHINFO_LOG_DESC)
    @RequestMapping(path="/savePushInfo",method = RequestMethod.POST)
    @ResponseBody
    @AuthAccessRequired
    public Object savePushInfo(OperateSavePushInfoDTO pushInfoDTO) {
        MessageVO messageVO;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pushInfoDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateSavePushInfoDTO.class, request);
            //数据添加对象不能为空
            if(null == pushInfoDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //判断项目编号
            if(StringUtils.isEmpty(pushInfoDTO.getProjectId())){
                return new MessageVO(OperateErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }

            //底层共用 运营添加接口（转换项目ID）
            pushInfoDTO.setProjectIds(pushInfoDTO.getProjectId());

            //物业系统，设置消息类型默认为物业
            pushInfoDTO.setInfoType(PushInfoTypeEnum.PROPERTY_OPERATE_PUSH_INFO_TYPE.getType());

            //判断推送时间
            if(StringUtils.isEmpty(pushInfoDTO.getBeginDate())){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_TIME_EMPTY_ERR.getErrorCode());
            }
            //判断推送内容
            if(StringUtils.isEmpty(pushInfoDTO.getInfoContent())){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_CONTENT_EMPTY_ERR.getErrorCode());
            }
            //发布平台 1代表App和代表小程序 3代表家半径物业宝APP
            //发布平台 最新更开 1代表App 2代表小程序 3代表家半径物业宝APP
            if ((null == pushInfoDTO.getPublishPlatform()) || (pushInfoDTO.getPublishPlatform() != 1 && pushInfoDTO.getPublishPlatform() != 2 && pushInfoDTO.getPublishPlatform() != 3)) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (StringUtils.isBlank(pushInfoDTO.getTitle())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Integer result = iOperatePushInfoServiceClient.savePushInfo(pushInfoDTO);
            if (result == -1) {
                return new MessageVO(BaseErrorCodeEnum.ERROR.getErrorCode(), "请等待工作人员执行完发布过程再操作");
            }
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业_物业管理_消息推送save】添加消息推送资源失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      修改推送消息内容。
     * <desc/>
     *
     * @param  pushInfoDTO 消息推送传输数据对象
     * @return
     * @author ZiXuan
     * @createDate 2017-10-30
     */
    @LoggerInfo(operateDesc = LogDescConstant.UPDATE_PUSHINFO_LOG_DESC)
    @RequestMapping(path="/updatePushInfo",method = RequestMethod.POST)
    @ResponseBody
    @AuthAccessRequired
    public Object updatePushInfo(OperateSavePushInfoDTO pushInfoDTO) {
        MessageVO messageVO;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pushInfoDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateSavePushInfoDTO.class, request);
            //数据更新对象不能为空
            if(null == pushInfoDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //判断消息ID
            if(StringUtils.isEmpty(pushInfoDTO.getPushInfoId())){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_ID_EMPTY_ERR.getErrorCode());
            }

            //判断推送时间
            if(StringUtils.isEmpty(pushInfoDTO.getBeginDate())){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_TIME_EMPTY_ERR.getErrorCode());
            }
            //判断推送内容
            if(StringUtils.isEmpty(pushInfoDTO.getInfoContent())){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_CONTENT_EMPTY_ERR.getErrorCode());
            }
            //发布平台 1代表App和代表小程序 3代表家半径物业宝APP
            //发布平台 最新更开 1代表App 2代表小程序 3代表家半径物业宝APP
            if ((null == pushInfoDTO.getPublishPlatform()) || (pushInfoDTO.getPublishPlatform() != 1 && pushInfoDTO.getPublishPlatform() != 2 && pushInfoDTO.getPublishPlatform() != 3)) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (StringUtils.isBlank(pushInfoDTO.getTitle())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Integer result = iOperatePushInfoServiceClient.updatePushInfo(pushInfoDTO);
            if (result == -1) {
                return new MessageVO(BaseErrorCodeEnum.ERROR.getErrorCode(), "请等待工作人员执行完发布过程再操作");
            }
            messageVO=new MessageVO(pushInfoDTO.getPushInfoId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业_物业管理_消息推送update】添加资源失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      批量删除推送消息内容。
     * <desc/>
     *
     * @param  pushInfoIds 消息ID
     * @return
     * @author ZiXuan
     * @createDate 2017-10-30
     */
    @LoggerInfo(operateDesc = LogDescConstant.DEL_PUSHINFO_LOG_DESC)
    @RequestMapping(path="/deletePushInfo",method = RequestMethod.POST)
    @ResponseBody
    @AuthAccessRequired
    public Object deletePushInfo(String pushInfoIds) {
        MessageVO messageVO;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pushInfoIds = request.getAttribute("pushInfoIds") == null ? null : request.getAttribute("pushInfoIds").toString();
            if(StringUtils.isEmpty(pushInfoIds)){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_ID_EMPTY_ERR.getErrorCode());
            }
            iOperatePushInfoServiceClient.deletePushInfo(pushInfoIds);
            messageVO=new MessageVO(pushInfoIds,BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业_物业管理_消息推送delete】添加资源失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      根据消息ID获取详细信息。
     * <desc/>
     *
     * @param  pushInfoId 消息
     * @return
     * @author ZiXuan
     * @createDate 2017-10-30
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(path = "/getPushInfo", method = RequestMethod.POST)
    public Object getPushInfo(String pushInfoId) {
        MessageVO messageVO;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pushInfoId = request.getAttribute("pushInfoId") == null ? null : request.getAttribute("pushInfoId").toString();
            if(StringUtils.isEmpty(pushInfoId)){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_ID_EMPTY_ERR.getErrorCode());
            }
            OperatePushInfoOneVO vo =iOperatePushInfoServiceClient.getPushInfo(pushInfoId);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),vo);
        } catch (Exception e) {
            throw new DataAccessException("【物业_物业管理_消息推送getOne】获取失败",e);
        }
        return messageVO;

    }

    /**
     * <desc>
     *      app查看消息详情
     * <desc/>
     *
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/07/24
     */
    @UnAuthAccessRequired
    @ResponseBody
    @RequestMapping("/getPushInfoByApp")
    public Object getPushInfoByApp(String pushInfoId) {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pushInfoId = request.getAttribute("pushInfoId") == null ? null : request.getAttribute("pushInfoId").toString();
            if (StringUtils.isBlank(pushInfoId)) {
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_ID_EMPTY_ERR.getErrorCode());
            }
            OperatePushInfoOneVO pushInfo = iOperatePushInfoServiceClient.getPushInfo(pushInfoId);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), pushInfo);
        } catch (Exception e) {
            throw new DataAccessException("【物业_物业管理_消息推送getPushInfoByApp】app查看消息详情失败", e);
        }
    }

    /**
     * <desc>
     *      获取消息推送分页信息。
     * <desc/>
     *
     * @param  pageDTO 请求参数
     * @return
     * @author ZiXuan
     * @createDate 2017-10-30
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(path = "/getPushInfoPage" , method = RequestMethod.POST)
    public Object getPushInfoPage(OperatePushInfoPageDTO pageDTO) {
        MessageVO messageVO;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pageDTO = HttpProtocolUtil.parseRequestParamToDTO(OperatePushInfoPageDTO.class, request);
            if(null == pageDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //判断项目编号
            if(StringUtils.isEmpty(pageDTO.getProjectId())){
                return new MessageVO(OperateErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }

            //物业系统，设置消息类型默认为物业
            pageDTO.setInfoType(PushInfoTypeEnum.PROPERTY_OPERATE_PUSH_INFO_TYPE.getType());

            Page<OperatePushInfoVO> voList= iOperatePushInfoServiceClient.getPushInfoPage(pageDTO);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),voList);
        }catch (Exception e) {
            throw new DataAccessException("【物业_物业管理_消息推送getPage】获取分页信息失败",e);
        }
        return messageVO;
    }
}
